package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.entity.WProduct;
import com.wms.dao.WProductDao;
import com.wms.entity.WUserStore;
import com.wms.mytool.Result;
import com.wms.service.WProductService;
import com.wms.vo.WProductInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商品信息表(WProduct)表服务实现类
 *
 * @author makejava
 * @since 2023-10-21 21:12:55
 */
@Service("wProductService")
public class WProductServiceImpl implements WProductService {
    @Resource
    private WProductDao wProductDao;

    /**
     * 查询所有
     * @return 实例对象
     */
    @Override
    public Result selectAll() {
        List<WProductInfo> wProductInfos = this.wProductDao.selectProductInfo();
        return new Result(200,wProductInfos,"查询成功");
    }

    /**
     * 添加商品
     * @param wProduct 商品信息
     * @return 实例对象
     */
    @Override
    public Result addProduct(WProduct wProduct) {
        wProduct.setCreateTime(new Date());
        wProduct.setUpdateTime(new Date());
        int i = this.wProductDao.insert(wProduct);
        if(i==1) {
            return new Result(200, i, "添加成功");
        }else{
            return new Result(400,null,"添加失败");
        }
    }

    /**
     * 修改商品信息
     * @param wProductInfo 商品对象
     * @return 实例对象
     */
    @Override
    public Result updateProductVo(WProductInfo wProductInfo) {
        wProductInfo.setUpdateTime(new Date());
        int i = this.wProductDao.updateProductInfo(wProductInfo);
        if(i==1) {
            return new Result(200, i, "修改成功");
        }else{
            return new Result(400,null,"修改失败");
        }
    }

    /**
     * 删除商品信息
     * @param id 商品id
     * @return 实例对象
     */
    @Override
    public Result deleteProduct(Integer id) {
        try{
            QueryWrapper<WProduct> qw = Wrappers.query();
            qw.eq("product_id", id);
            int i = this.wProductDao.delete(qw);
            if(i==1) {
                return new Result(200, i, "删除成功");
            }else{
                return new Result(400,null,"删除失败");
            }
        }catch (Exception e){
            return new Result(400,null,"存在关联出入库信息，不可删除");
        }
    }
}
